package com.tanger.pay.wechat;

import com.github.wxpay.sdk.WXPayConfig;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @Description TODO
 * @Author Hchenbin
 * @Date 2024-01-05 10:21
 * @Version 1.0
 */
public class WXPayFactory implements WXPayConfig {

    private byte[] certData = null;

    @Override
    public String getAppID() {
        return WeChatPayConfig.appId;
    }

    @Override
    public String getMchID() {
        return WeChatPayConfig.mchId;
    }

    @Override
    public String getKey() {
        return WeChatPayConfig.key;
    }

    @Override
    public InputStream getCertStream() {
        return new ByteArrayInputStream(certData);
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    public WXPayFactory() {
        try {
            //证书路径
            InputStream is = new FileInputStream(WeChatPayConfig.certPath);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] bs = new byte[1024];
            int cnt = -1;
            while ((cnt = is.read(bs)) != -1) {
                baos.write(bs, 0, cnt);
            }
            is.close();
            certData = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
